package action;

import global.Constants;
import global.beanUtils.BeanUtils;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.FeedFavoriteService;
import util.ServletHelp;
import util.StringUtils;
import domain.FeedFavorite;

/**
 * 收藏订阅Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class FeedFavoriteAction extends BaseAction {

	/**
	 * 订阅Service
	 * */
	private FeedFavoriteService feedFavoriteService = null;

	/**
	 * @param feedFavoriteService
	 *            the feedFavoriteService to set
	 */
	public void setFeedFavoriteService(FeedFavoriteService feedFavoriteService) {
		this.feedFavoriteService = feedFavoriteService;
	}


	/**
	 * 查询收藏订阅列表
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		FeedFavorite paramFeedFavorite = new FeedFavorite();
		BeanUtils.populate(paramFeedFavorite, map);

		// 取得订阅列表
		JSONArray datas = new JSONArray();
		List<FeedFavorite> feedFavoriteList = feedFavoriteService
				.selectByCriteriaWithBLOBsForPaging(paramFeedFavorite);
		if(null!=feedFavoriteList){
			datas = JSONArray.fromObject(feedFavoriteList);
		}
		int count = feedFavoriteService.countByCriteria(paramFeedFavorite);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		res.put("datas", datas);
		res.put("results", count);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}


	/**
	 * 取得单个订阅信息
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getFeedFavorite(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 订阅ID
		String feedFavoriteId = request.getParameter("feedFavoriteId");
		// 取得订阅分类
		FeedFavorite feedFavorite = feedFavoriteService.selectByPrimaryKey(Integer.parseInt(feedFavoriteId));

		// 返回结果
		JSONObject res = JSONObject.fromObject(feedFavorite);
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存收藏的订阅信息
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		FeedFavorite feedFavorite = new FeedFavorite();
		BeanUtils.populate(feedFavorite, map);
		feedFavorite.setDelflag(Constants.DEL_FLAG_1);
		feedFavorite.setStatus(Constants.STATUS_1);
		
		// 添加订阅
		feedFavoriteService.insert(feedFavorite);

		// 构造查询结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		String result = res.toString();
		// 返回查询结果
		ServletHelp.outRequestForJson(request, response, result);
		return null;
	}

	/**
	 * 删除收藏订阅
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 订阅ID
		String feedFavoriteIdStr = request.getParameter("feedFavoriteIds");
		// 封装订阅列表
		if (StringUtils.isNotEmpty(feedFavoriteIdStr)) {
			// 删除订阅
			feedFavoriteService.delete(feedFavoriteIdStr);
		}
		
		// 构造查询结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		String result = res.toString();
		// 返回查询结果
		ServletHelp.outRequestForJson(request, response, result);
		return null;
	}
}
